﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Configuration;
using System.IO;
using System.Drawing.Imaging;
using System.Net;
using CarLoanSystem.Web.Comman;
using CarLoanSystem.DAL;
using CarLoanSystem.Entity;

namespace CarLoanSystemWeb.Page.EditProfile
{
    public partial class wuc_edit_profile : UserControlManageForm
    {
        public event EventHandler InfoMessege;
        public event EventHandler ErrorMessege;
        private static CustomerItem cusItem;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                cusItem = Session["Member"] as CustomerItem;
                this.txtAddress.Text = cusItem.CUSTOMER_ADDRESS;
                this.txtAddressWork.Text = cusItem.CUSTOMER_WORK_ADDRESS;
                this.txtAge.Text = cusItem.CUSTOMER_AGE.ToString();
                this.txtJob.Text = cusItem.CUSTOMER_JOB;
                this.txtLastName.Text = cusItem.CUSTOMER_LASTNAME;
                this.txtName.Text = cusItem.CUSTOMER_NAME;
                this.txtPerId.Text = cusItem.CUSTOMER_PERSONAL_ID;
                this.txtSalary.Text = cusItem.CUSTOMER_SARARY.ToString();
                this.txtTel.Text = cusItem.CUSTOMER_TEL;
                this.txtTelWork.Text = cusItem.CUSTOMER_WORK_TEL;
                this.txtName.Focus();
                BlindDataTable();
                if (gvListBondsman.DataSource != null)
                {
                    this.btnBondsman.Visible = false;
                    this.lblBonsman.Visible = true;
                }
            }
        }

        private void BlindDataTable()
        {
            this.gvListDept.DataSource = CustomerDebtInfoDAL.LoadCusDeptAll(cusItem.CUSTOMER_ID);
            this.gvListDept.DataBind();

            this.gvListDoc.DataSource = CustomerDocInfoDAL.LoadCusDocAll(cusItem.CUSTOMER_ID);
            this.gvListDoc.DataBind();

            this.gvListBondsman.DataSource = BondsmanInfoDAL.LoadAllBondsman(cusItem.CUSTOMER_ID);
            this.gvListBondsman.DataBind();
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                cusItem.CUSTOMER_ADDRESS = this.txtAddress.Text;
                cusItem.CUSTOMER_AGE = Convert.ToInt32(this.txtAge.Text);
                cusItem.CUSTOMER_JOB = this.txtJob.Text;
                cusItem.CUSTOMER_LASTNAME = this.txtLastName.Text;
                cusItem.CUSTOMER_NAME = this.txtName.Text;
                cusItem.CUSTOMER_PERSONAL_ID = this.txtPerId.Text;
                if (this.txtSalary.Text == "")
                {
                    cusItem.CUSTOMER_SARARY = 0;
                }
                else
                {
                    cusItem.CUSTOMER_SARARY = Convert.ToInt32(this.txtSalary.Text);
                }
                cusItem.CUSTOMER_TEL = this.txtTel.Text;
                cusItem.CUSTOMER_WORK_ADDRESS = this.txtAddressWork.Text;
                cusItem.CUSTOMER_WORK_TEL = this.txtTelWork.Text;
                CustomerInfoDAL.Update(cusItem);
                if (InfoMessege != null)
                {
                    InfoMessege(this, EventArgs.Empty);
                }
            }
            catch(Exception ex)
            {
                if (ErrorMessege != null)
                {
                    ErrorMessege(this, EventArgs.Empty);
                }
            }
        }

        protected void gvListDept_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvListDept.PageIndex = e.NewPageIndex;
            gvListDept.DataBind();
        }

        protected void gvListDept_EditCommand(object sender, GridViewEditEventArgs e)
        {
            Response.Redirect("EditProfileDeptPage.aspx?id=" + gvListDept.DataKeys[e.NewEditIndex].Values[0].ToString());
        }

        protected void AddDept_Click(object sender, EventArgs e)
        {
            Response.Redirect("EditProfileDeptPage.aspx");
        }

        protected void gvListDept_Bound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                if (e.Row.Cells[4].Text.ToString().Equals("A"))
                    e.Row.Cells[4].Text = "ใช้งาน";
                else
                    e.Row.Cells[4].Text = "ไม่ใช้งาน";
            }
        }

        protected void AddDoc_Click(object sender, EventArgs e)
        {
            Response.Redirect("EditProfileDocPage.aspx");
        }

        protected void gvListDoc_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            CustomerDocInfoDAL.Delete(Convert.ToInt32(gvListDoc.DataKeys[e.RowIndex].Values[0].ToString()));
            BlindDataTable();
        }

        protected void gvListDoc_EditCommand(object sender, GridViewEditEventArgs e)
        {
            try
            {
                CustomerDocItem item = CustomerDocInfoDAL.LoadCusDoc(Convert.ToInt32(gvListDoc.DataKeys[e.NewEditIndex].Values[0].ToString()));
                if (item != null)
                {
                    Response.AddHeader("Content-type", item.CUSTOMER_DOC_EXTENSION);
                    Response.AddHeader("Content-Disposition", "attachment; filename=" + item.CUSTOMER_DOC_NAME + item.CUSTOMER_DOC_EXTENSION);
                    Response.BinaryWrite(item.CUSTOMER_DOC_FILE);
                    Response.Flush();
                    Response.End();
                }
                else
                {
                    DataTable dx = (DataTable)gvListDoc.DataSource;
                    Response.AddHeader("Content-type", dx.Rows[e.NewEditIndex]["CUSTOMER_DOC_EXTENSION"].ToString());
                    Response.AddHeader("Content-Disposition", "attachment; filename=" + dx.Rows[e.NewEditIndex]["CUSTOMER_DOC_NAME"].ToString() + dx.Rows[e.NewEditIndex]["CUSTOMER_DOC_EXTENSION"].ToString());
                    Response.BinaryWrite((byte[])dx.Rows[e.NewEditIndex]["CUSTOMER_DOC_FILE"]);
                    Response.Flush();
                    Response.End();
                }
            }
            catch (Exception ex)
            {
                ex.ToString();
            }
        }

        protected void gvListDoc_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvListDept.PageIndex = e.NewPageIndex;
            gvListDept.DataBind();
        }

        protected void gvListBondsman_EditCommand(object sender, GridViewEditEventArgs e)
        {
            Response.Redirect("EditProfileDonsmanPage.aspx?id=" + gvListBondsman.DataKeys[e.NewEditIndex].Values[0].ToString());
        }

        protected void gvListBondsman_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvListBondsman.PageIndex = e.NewPageIndex;
            gvListBondsman.DataBind();
        }

        protected void AddBons_Click(object sender, EventArgs e)
        {
            Response.Redirect("EditProfileDonsmanPage.aspx");
        }
    }
}